package 临时工具.shangyu1;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.apache.commons.collections4.ListUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 导第三方运维任务
 */
@Data

public class 导第三方运维任务 implements Serializable {
    public static void main(String[] args) {
        try {
            String urlStr = "http://39.184.194.199:8281/api_8282/drainage/opsTask/taskUpkeepList?page=1&limit=100000&taskType=2&status=10&startTaskTime=2024-04-01&endTaskTime=2025-03-31";
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setRequestMethod("GET");
            conn.setRequestProperty("token", "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImNyZWF0ZWQiOjE3NDQ3Njg1OTAyNTAsImV4cCI6MTc0NTM3MzM5MCwidXNlcklkIjoxfQ.nMeqBankM1AoawHqTwalYWSBr5sNwUwro2I_8TDwsyZfXm6Y7ijcm7CDE-Xrm_Jn8EiGWIZWP7PJUEZHqSKmTQ");

            int responseCode = conn.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(), "UTF-8"));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            JSONObject jsonObject = JSONObject.parseObject(response.toString());
            jsonObject=jsonObject.getJSONObject("data");
            JSONArray data = jsonObject.getJSONArray("list");
            ArrayList<String> idList = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                JSONObject jsonObject1 = data.getJSONObject(i);
                idList.add(jsonObject1.getString("id"));
            }
            List<List<String>> partition = ListUtils.partition(idList, 10);
            for (int i = 0; i < partition.size(); i++) {
                getTaskList(partition.get(i),i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void getTaskList(List<String> ids,Integer i) {
        if ((i+1)!=84){
return;
        }
        try {
            URL url = new URL("http://127.0.0.1:8190/drainage/opsTask/exportOneOpsTaskInfoByIds");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            // 设置请求方法和头部
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("token", "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImNyZWF0ZWQiOjE3NDQ3Njg1OTAyNTAsImV4cCI6MTc0NTM3MzM5MCwidXNlcklkIjoxfQ.nMeqBankM1AoawHqTwalYWSBr5sNwUwro2I_8TDwsyZfXm6Y7ijcm7CDE-Xrm_Jn8EiGWIZWP7PJUEZHqSKmTQ");
            conn.setDoOutput(true);

            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("imgFlag",false);
            stringObjectHashMap.put("ids",ids);
                // 构造 JSON 参数
            String jsonInput =JSONObject.toJSONString(stringObjectHashMap);

            // 写入 JSON 请求体
            try (OutputStream os = conn.getOutputStream()) {
                byte[] input = jsonInput.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // 下载并保存 Excel 文件
            try (InputStream in = conn.getInputStream();
                 FileOutputStream out = new FileOutputStream("C:\\Users\\瑾瑜\\Desktop\\sql\\20230621\\"+(i+1)+"output.xlsx")) {

                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = in.read(buffer)) != -1) {
                    out.write(buffer, 0, bytesRead);
                }
            }

            System.out.println("Excel 下载完成，文件名："+(i+1)+"output.xlsx");

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("------------------Excel 下载失败，文件名："+(i+1)+"output.xlsx");

        }
    }
}





